package transfer;

import java.io.Serializable;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;

public class cipher {
    
    private Key key;
    
    public cipher(){
        KeyGenerator generator;
        try {
            generator = KeyGenerator.getInstance("DES");
            generator.init(new SecureRandom());
            key = generator.generateKey();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }            
    }
    
    public Key getKey(){
        return key;
    }
 
    public SealedObject encriptar(Serializable o){
        try {
            //Creo el objeto para encriptar
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            SealedObject result = new SealedObject(o,cipher);
            return result; 
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
    
}
